//
//  FamilyHeartSideView.swift
//  aj
//
//  Created by leeqb on 2/18/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class FamilyHeartSideView: UIView {
    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet var cntView: UIView!
    
    var familyData: [NSObject : AnyObject]! {
        didSet {
            print(familyData)
            /*let userList = familyData["userList"] as? [[NSObject : AnyObject]]
            print("家庭成员数：\(familyData["userList"])")
            var callNames = ""
            if userList?.count > 0 {
                self.hidden = false
                for i in 0...userList!.count - 1 {
                    let ele = userList![i]
                    if i == 0 {
                        callNames += ele["callName"] as? String ?? ""
                        head1.sd_setImageWithURL(NSURL(string: ele["photo"] as! String))
                    }
                    
                    if i == 1 {
                        callNames += ele["callName"] as? String ?? ""
                        head2.sd_setImageWithURL(NSURL(string: ele["photo"] as! String))
                    }
                }
            }
            textLabel.text = callNames*/
            let userList = familyData["userList"] as? [[NSObject : AnyObject]]
            if userList?.count > 0 {
                self.hidden = false
                
                let headWidth: CGFloat = 20
                let headHeight: CGFloat = 20
                var callNames = ""
                for var i in 0...userList!.count - 1 {
                    let ele = userList![i]
                    let head = UIImageView()
                    head.backgroundColor = RGB(237, g: 237, b: 237)
                    head.layer.cornerRadius = headWidth / 2
                    head.layer.masksToBounds = true
                    head.sd_setImageWithURL(NSURL(string: ele["photo"] as? String ?? ""))
                    self.addSubview(head)
                    
                    switch i {
                    case 0:
                        head.frame = CGRect(x: 4, y: 46, width: headWidth, height: headHeight)
                    case 1:
                        head.frame = CGRect(x: 6 + headWidth, y: 46, width: headWidth, height: headHeight)
                    case 2:
                        head.frame = CGRect(x: 4, y: 46 + headHeight, width: headWidth, height: headHeight)
                    case 3:
                        head.frame = CGRect(x: 6 + headWidth, y: 46 + headHeight, width: headWidth, height: headHeight)
                    default:
                        head.frame = CGRectZero
                        break
                    }
                    i++
                    
                    if ele["callName"] is NSNull {
                        callNames += "我"
                    } else {
                        callNames += ele["callName"] as? String ?? ""
                    }
                    callNames += "\n"
                }
                textLabel.text = callNames
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initSelf()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initSelf()
    }
    
    private func initSelf() {
        LX("FamilyHeartSideView", owner: self)
        self.addSubview(cntView)
        
        self.hidden = true
    }
    
}
